package com.thrsdk.utils.local;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;


import com.dy.google.gson.Gson;
import com.thrsdk.THFSDKClient;
import com.thrsdk.utils.JsonUtils;

import java.io.Serializable;

public class SpUtils {
    private static SpUtils instance;
    private final String FILE_NAME = "fusion_dy_operation_sdk";
    private final SharedPreferences sharedPreferences;
    private final SharedPreferences.Editor editor;

    private SpUtils() {
        sharedPreferences = THFSDKClient.getInstance().getContext().getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

    }

    public static SpUtils getInstance() {
        if (instance == null) {
            instance = new SpUtils();
        }
        return instance;
    }

    public void putSerializable(String name, Serializable obj) {
        editor.putString(name, new Gson().toJson(obj));
        editor.commit();
    }

    /**
     * 存储
     */
    public void put(String key, Object object) {
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        } else {
            if (object != null) {
                editor.putString(key, object.toString());
            }
        }
        editor.commit();
    }

    /**
     * 获取保存的数据
     */
    public Object get(String key, Object defaultObject) {
        if (defaultObject instanceof String) {
            return sharedPreferences.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sharedPreferences.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sharedPreferences.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sharedPreferences.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sharedPreferences.getLong(key, (Long) defaultObject);
        } else {
            return sharedPreferences.getString(key, null);
        }
    }

    public <Result> Result get(String name, Class<Result> obj) {
        try {
            String data = sharedPreferences.getString(name, "");
            if (!TextUtils.isEmpty(data)) {
                return JsonUtils.strTobean(data, obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除一条信息
     */

    public void remove(String tag) {
        editor.remove(tag);
        editor.commit();
    }
}